import { getFileAndFolder } from '../../api/index.js'
import { dateFormat } from '../../utils/index.js'
Page({
  data() {
    return {
      tiYiList: [],
      folderList: [],
      fileList: [],
      fileAndFolderId: '',
      loading: false,
      tiYiSelectedList: [],
   
      folderPathes: [{ name: "全部文件", id: '' }],

      btnStyle: {
        'border-radius': '20px',
        'height': '40px',
        'font-size': '18px',
        'line-height': '40px',
        'color': '#FFFFFF',
        'text-shadow': '0px 1px 2px rgba(0, 0, 0, .1)',
        'border': 'none',
        'width': '30%',
        'background': '#3069DC'
      },
      backBtnStyle:{
        'margin-right':'10px',
        'border-radius': '20px',
        'height': '40px',
        'font-size': '18px',
        'line-height': '40px',
        'color': '#666',
        'text-shadow': '0px 1px 2px rgba(0, 0, 0, .1)',
        'border': '1px solid #666',
        'width': '30%',
        'background': '#fff'
      },
    }
  },
  methods: {
    gotoBack(){
      this.$router.replace({ path: '/pages/info/index' })
    },
    sortList(sortVal) {
      if (sortVal === 2) {
        this.fileList.sort((a, b) => {
          let atime = new Date(a.time).getTime()
          let btime = new Date(b.time).getTime()
          return btime - atime
        })
        this.folderList.sort((a, b) => {
          let atime = new Date(a.time).getTime()
          let btime = new Date(b.time).getTime()
          return btime - atime
        })
      } else if (sortVal === 1) {
        this.fileList.sort((a, b) => {
          return b.size - a.size
        })
      } else if (sortVal === 0) {
        this.fileList.sort((a, b) => {
          return a.name.localeCompare(b.name)
        })
        this.folderList.sort((a, b) => {
          return a.name.localeCompare(b.name)
        })
      }
      this.customList = [...this.folderList, ...this.fileList]
    },
    async getFileAndFolder() {
      let panType = this.panType
      this.loading = true
      const { data } = await getFileAndFolder({
        panType,
        fileAndFolderId: this.fileAndFolderId=='/全部文件' ? '' :this.fileAndFolderId
      })


      if (data.code === 500&&data.msg==='用户登录已超时，请重新登录') {
       
        this.$router.replace('/pages/login/index')
        return
      }
      this.loading = false
      let folderList = data.data.folderList.map(val => {
        val.checkValue = false
        val.time = val.updateTime
        return val
      })
      let fileList = data.data.fileList.map(val => {
        val.checkValue = false
        val.time = val.updateTime
        return val
      })
      this.folderList = folderList
      this.fileList = fileList
      
      this.sortList(2)
      this.tiYiList = [...folderList, ...fileList]
    },

    checkChangeValue(val, file) {
      let item = this.tiYiList.find((item) => {
        return item.id === file.id
      })
      item.checkValue = val

      this.tiYiSelectedList = this.tiYiList.filter((item) => {
        return item.checkValue
      })

      console.log(this.tiYiSelectedList);
      ok.setH5AppStorage({
        key: 'downFiles',
        data: [...this.tiYiSelectedList]
      }).catch(err => {
        console.log("接口调用异常", err);
      });
    },
    // selectedFileByTianYiDisk() {
    //   this.showfileDialog = true
    //   this.getFileAndFolder()
    // },
    tiYiPathClick(val) {
      let pathes = this.folderPathes
      let cur = pathes[pathes.length - 1]
      if (cur.id == val.id) {
        return
      }
      this.fileAndFolderId = val.id
      if(this.panType=='2'){
        this.fileAndFolderId = val.parentId ? val.parentId + '/' + val.name : '/' + val.name
      }
      while (true) {
        let cur = pathes.pop()
        // 说明这是最后一个
        if (cur.id == val.id) {
          pathes.push(cur)
          break
        }
      }
      this.tiYiSelectedList = []
      this.getFileAndFolder()
    },
    backFilePath() {
      console.log("dianji le ", this.folderPathes.length - 2 < 0);
      if (this.folderPathes.length - 2 < 0) return
      this.tiYiPathClick(this.folderPathes[this.folderPathes.length - 2])
    },
    tiYiFolderClick(item) {
      if (!item.fileType) {
        this.fileAndFolderId = item.id
        if(this.panType=='2'){
          this.fileAndFolderId = item.parentId ? item.parentId + '/' + item.name : '/' + item.name
        }
        this.folderPathes.push({
          id: item.id,
          name: item.name,
          parentId:item.parentId
        })
        this.tiYiSelectedList = []
        this.getFileAndFolder()
      }
    },
    confirmMoreFile() { 
      this.$router.replace({ path: '/pages/yunpanlist/index' })
    },
  },
  mounted() {
    ok.getH5AppStorage({
      key: 'panType'
    }).then(res => {
      this.panType = res.data.data
      this.getFileAndFolder()
    }).catch(err => {
      console.log("接口调用异常", err);
    });
  }
});
